package com.lin.csdn.controller;

import com.lin.csdn.service.impl.CommentService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/commentServlet")
public class CommentServlet extends HttpServlet {
    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     *
     * 0：根据博客id获取评论数据 BlogIdGetComment
     * 1：根据用户获取用户发表的评论 UserNameGetComment
     * 2：根据评论id删除评论 DeleteCommentById
     * 3：创建新的评论 AddNewComment
     * 4：搜索通知 SeekNotify
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding ("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        //建立功能代表的code调用
        String BlogIdGetComment="0";//通过博客id获取评论
        String UserNameGetComment="1";//通过用户名获取评论
        String DeleteCommentById="2";//根据评论id删除评论
        String AddNewComment="3";//添加新的评论
        String SeekNotify="4";//搜索通知
        //获取req数据
        String code=req.getParameter("code");
        List<Object> commentData = null;
        CommentService commentService=new CommentService();
        PrintWriter writer = resp.getWriter(); //响应流
        JSONArray array=new JSONArray();

        //根据需求调用各自的方法
        if(code.equals(BlogIdGetComment)){
            String blogId=req.getParameter("blogId");
            String Page=req.getParameter("currentPage");
            int currentPage=Integer.parseInt(Page);
            //设置每次查询的条数
            int pageSize=10;
            int beginIndex = (currentPage - 1) * pageSize;
            commentData=commentService.blogIdSearch(blogId,beginIndex,pageSize);
        }
        else if(code.equals(UserNameGetComment)){
            String userName=req.getParameter("userName");
            String Page=req.getParameter("currentPage");
            int currentPage=Integer.parseInt(Page);
            //设置每次查询的条数
            int pageSize=10;
            int beginIndex = (currentPage - 1) * pageSize;
            commentData=commentService.userNameSearch(userName,beginIndex,pageSize);
        }
        else if(code.equals(DeleteCommentById)){
            JSONObject object=new JSONObject();
            String commentId=req.getParameter("commentId");
            commentService.commentIdDelete(commentId);
            object.put("result","删除成功");
            writer.print(object);
        }
        else if(code.equals(AddNewComment)){
            JSONObject object=new JSONObject();
            String userName=req.getParameter("userName");
            String blogId=req.getParameter("blogId");
            String context=req.getParameter("context");
            int newCommentId=commentService.addComment(context,blogId,userName);
            object.put("commentId",newCommentId);
            writer.print(object);
        }
        else if(code.equals(SeekNotify)){
            String userName=req.getParameter("userName");
            List<Object> data=commentService.searchNotify(userName);
            for(int i=0;i<data.size();i++){
                JSONObject object=new JSONObject();
                object.put("context",data.get(i));
                i++;
                object.put("date", data.get(i));
                array.put(object);
            }
            writer.print(array);
        }
        if(code.equals(BlogIdGetComment)||code.equals(UserNameGetComment)) {
            for (int i = 0; i < commentData.size(); i++) {
                JSONObject object=new JSONObject();
                object.put("id", commentData.get(i));
                i++;
                object.put("blogId", commentData.get(i));
                i++;
                object.put("context", commentData.get(i));
                i++;
                object.put("date", commentData.get(i));
                i++;
                object.put("username", commentData.get(i));
                array.put(object);
            }
            writer.print(array);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
